package com.ly.lypopupwindowdemo.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.adapter.MyFragmentPageAdapter;
import com.ly.lypopupwindowdemo.fragment.Fragment1;
import com.ly.lypopupwindowdemo.fragment.Fragment2;
import com.ly.lypopupwindowdemo.fragment.Fragment3;
import com.ly.lypopupwindowdemo.fragment.Fragment4;
import com.ly.lypopupwindowdemo.fragment.Fragment5;
import com.ly.lypopupwindowdemo.pagerTrans.DepthPageTransformer;

import java.util.ArrayList;
import java.util.IllegalFormatCodePointException;
import java.util.List;

public class IndicatorActivity extends AppCompatActivity {

    //定义Fragment
    private Fragment fragment1;
    private Fragment fragment2;
    private Fragment fragment3;
    private Fragment fragment4;
    private Fragment fragment5;
    //定义FragmentManager
    private FragmentManager fragmentManager;

    //定义组件
    private ViewPager viewPager;
    private List<Fragment> fragmentLists;
    private MyFragmentPageAdapter adapter;


    private LinearLayout llContainer;
    private ImageView redPoint;
    private int mPointWidth;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_indicator);

        //初始化界面组件
        init();
        //初始化ViewPager
        initViewPager();
    }

    private void initViewPager() {
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();
        fragment5 = new Fragment5();

        fragmentLists = new ArrayList<Fragment>();
        fragmentLists.add(fragment1);
        fragmentLists.add(fragment2);
        fragmentLists.add(fragment3);
        fragmentLists.add(fragment4);
        fragmentLists.add(fragment5);
        //获取FragmentManager对象
        fragmentManager = getSupportFragmentManager();
        //获取FragmentPageAdapter对象
        adapter = new MyFragmentPageAdapter(fragmentManager, fragmentLists);


        //初始化小圆点
        for (int i=0;i<fragmentLists.size();i++){
            ImageView point = new ImageView(getApplicationContext());
            point.setBackgroundResource(R.drawable.light_gray_point);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            if (i>0){
                params.leftMargin=35;
            }
            point.setLayoutParams(params);
            llContainer.addView(point);
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //页面滑动事件
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //滑动便宜百分比 偏移量 positionOffset  0-1百分比
                //当前位置 position
                //用页面滑动百分比乘以两个圆点的距离就是小红点的位置

                /**
                 * 重新修改布局参数
                 */
                int leftMargin  = (int) (mPointWidth*positionOffset+mPointWidth*position);

                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) redPoint.getLayoutParams();
                params.leftMargin = leftMargin;
                redPoint.setLayoutParams(params);

            }

            @Override
            public void onPageSelected(int position) {

                if(position==fragmentLists.size()-1){

                }else{

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //onCreate()之后才能 measure--layout--draw
        //计算两个小圆点的距离
//        mPointWidth = llContainer.getChildAt(1).getLeft()-llContainer.getChildAt(0).getLeft();
//System.out.print("圆点距离"+mPointWidth);
        /**
         *  所以此时取不到圆点距离，是0.不能在onCreate里获取位置相关信息。因为根本不执行
         *  mearsure这些方法
         *  getWidth()getHeight（）拿不到
         *  所以应该监听 layout执行结束的事件，结束之后在获取位置，计算圆点信息
         */

        /**
         * 如何监听到layout事件呢，首先我们可以获取视图树
         * 获取视图树
         * 画视图的时候会从根布局开始层层向下绘制 可以通过sdk里的tools里的工具查看
         *
         */

        redPoint.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            //layout方法执行结束后，会回掉这个方法
            @Override
            public void onGlobalLayout() {
                //在这里再计算圆点的距离
                //取消视图监听
                redPoint.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                mPointWidth = llContainer.getChildAt(1).getLeft()-llContainer.getChildAt(0).getLeft();
//                Log.d("ZHBJ",mPointWidth+"");

            }
        });


    }

    private void init() {

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        redPoint = (ImageView) findViewById(R.id.redPoint);
        llContainer = (LinearLayout) findViewById(R.id.ll_container);



    }
}
